package com.hnkywl.sts.action.flow.assign;

import com.hnkywl.sts.entity.jhgl.Zgzjjh;
import com.hnkywl.sts.service.jhgl.ZjjhService;
import com.hnkywl.sts.util.ApplicationInstance;
import com.hnkywl.sts.entity.flow.FlowTask;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.service.flow.FlowService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.YgService;
import org.jbpm.api.model.OpenExecution;
import org.jbpm.api.task.Assignable;
import org.jbpm.api.task.AssignmentHandler;
import org.jbpm.pvm.internal.model.ExecutionImpl;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-5-8
 * Time: 下午2:16
 * To change this template use File | Settings | File Templates.
 */
public class AssginZgzjjhTask implements AssignmentHandler {
    private String currentOrgan;

    @Override
    public void assign(Assignable assignable, OpenExecution execution) throws Exception {
        ExecutionImpl executionImpl = (ExecutionImpl) execution;
        String activityname = executionImpl.getActivityName();

        FlowService flowService = ApplicationInstance.createFlowService();
        /**
         * 这里的用户通过角色获得
         */
        List<FlowTask> flowTaskList;
        ZjjhService zjjhService = ApplicationInstance.createZjjhService();
        if (activityname.equals("本公司审核")) {
            Zgzjjh zgzjjh = zjjhService.findZgzjjhDetailByProcessInstance(execution.getProcessInstance().getId());
            if (zgzjjh != null)
                flowTaskList = flowService.findAllTaskYgBack("自购资金计划流程", activityname, currentOrgan, zgzjjh.getSbbm().getId(), zgzjjh.getSbgs().getId());
            else
                flowTaskList = flowService.findAllTaskYg("自购资金计划流程", activityname, currentOrgan);
        } else
            flowTaskList = flowService.findAllTaskYg("自购资金计划流程", activityname, currentOrgan);
        if (flowTaskList != null && flowTaskList.size() > 0) {
            for (FlowTask flowTask : flowTaskList) {
                assignable.addCandidateUser(flowTask.getBmId().toString());
            }
        } else {
            assignable.addCandidateUser(YgService.getCurrentYgZgGsId().toString());
        }
    }
}
